package zmq

import (
	"fmt"
	"time"
	"usplatformserver/global"

	zmq "github.com/pebbe/zmq4"
)

// 发送消息
func SendMessage(addr, msg string) (string, error) {

	fmt.Println("addr: ", addr, "msg: ", msg)

	req, err := zmq.NewSocket(zmq.REQ)
	if err != nil {
		global.Logger.Info("zmq.NewSocket failed")
		return "", err
	}
	//建立连接
	if err = req.Connect(addr); err != nil {
		global.Logger.Info("req.Connect failed")
		return "", err
	}
	defer req.Close()

	req.SetRcvbuf(1024 * 10)
	req.SetRcvtimeo(5 * time.Second) //设置5秒超时

	//发送消息
	_, err = req.Send(msg, 0)
	if err != nil {
		global.Logger.Info("req.Send failed")
		return "", err
	}
	//30ms后回复
	time.Sleep(30 * time.Millisecond)
	//接收消息
	rcv, err := req.Recv(0)
	if err != nil {
		global.Logger.Info("req.Recv failed")
		return "", err
	}

	return rcv, nil
}
